package com.cd826dong.clouddemo.api;

import com.cd826dong.clouddemo.event.MyBusEvent;
import com.cd826dong.clouddemo.util.ApplicationContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/bus/events")
public class UserEventEndpoint {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @RequestMapping(value = "/{eventType}", method = RequestMethod.GET)
    public Boolean publishEvent(@PathVariable String eventType){
// 构建一个自定义的事件， 并通过ApplicationContext发布
        ApplicationContext applicationContext =
                ApplicationContextHolder.getApplicationContext();
        MyBusEvent myBusEvent = new MyBusEvent(this,
                applicationContext.getId(), "*:**", eventType);
// 通过ApplicationContext.publishEvent方法就可以发布一个事件
// 不论事件是否是远程事件
        applicationContext.publishEvent(myBusEvent);
        this.logger.debug("publish remote application event = {}", myBusEvent);
        return Boolean.TRUE;
    }
}
